package irc;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.logging.Level;
import java.util.logging.Logger;
import misc.UserPrivilage;

/**
 * Klasa usera. Reprezentuje każdego użytkonika na kanale
 * @author adrian
 */
public class User {

    /**
     * Referencja do kanału na którym się znajduje user
     */
    private Channel channel;
    /**
     * Nazwa uzytkownika
     */
    private String name;
    /**
     * Emial uzytkownika
     */
    private String email;
    /**
     * Host uzytkownika
     */
    private String host;
    /**
     * nick uzytkownika
     */
    private String nick;
    /**
     * zaszyfrowane hasło do profilu
     */
    private String password;

    /**
     * Metoda zwraca ZAHASHOWANE hasło
     * @return 
     */
    public String getPassword() {
        return password;
    }

    /**
     * Metoda hashuje hasło w najbanalnieszy sposób, żeby nie można było
     * go odczytać z pliku konfiguracyjengo
     * @param password 
     * @throws UnsupportedEncodingException
     */
    public void setPassword(String password) throws UnsupportedEncodingException {
        try {
            this.password = hashPassword(password);
        } catch (NoSuchAlgorithmException ex) {
            Logger.getLogger(User.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /**
     * Metoda sprawdza czy podane hasło zgadza sie z haslem w pliku konfiguracyjnym
     * @param passwordTyped
     * @return
     * @throws NoSuchAlgorithmException, UnsuportedEncodingException
     */
    public boolean checkPassword(String passwordTyped) throws NoSuchAlgorithmException, UnsupportedEncodingException {
        if (getPassword().equals(hashPassword(passwordTyped))) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * Metoda hashuje hasło
     * @param password
     * @return
     * @throws NoSuchAlgorithmException
     * @throws UnsupportedEncodingException 
     */
    private String hashPassword(String password) throws NoSuchAlgorithmException, UnsupportedEncodingException {
        byte[] bytes = password.getBytes("UTF-8");
        MessageDigest md = MessageDigest.getInstance("MD5");
        return md.digest(bytes).toString();
    }
    /**
     * Prawa uzytkownika na jakich pozostaje na kanale
     * @see UserPrivilage     
     */
    private UserPrivilage privilage;

    /**
     * @return the channel
     */
    public Channel getChannel() {
        return channel;
    }

    /**
     * @param channel the channel to set
     */
    public void setChannel(Channel channel) {
        this.channel = channel;
    }

    /**
     * @return the name
     */
    public String getName() {
        return name;
    }

    /**
     * @param name the name to set
     */
    public void setName(String name) {
        this.name = name;
    }

    /**
     * @return the privilage
     */
    public UserPrivilage getPrivilage() {
        return privilage;
    }

    /**
     * @param privilage the privilage to set
     */
    public void setPrivilage(UserPrivilage privilage) {
        this.privilage = privilage;
    }

    /**
     * @return the email
     */
    public String getEmail() {
        return email;
    }

    /**
     * @param email the email to set
     */
    public void setEmail(String email) {
        this.email = email;
    }

    /**
     * @return the host
     */
    public String getHost() {
        return host;
    }

    /**
     * @param host the host to set
     */
    public void setHost(String host) {
        this.host = host;
    }

    /**
     * @return the nick
     */
    public String getNick() {
        return nick;
    }

    /**
     * @param nick the nick to set
     */
    public void setNick(String nick) {
        this.nick = nick;
    }
}
